home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / cacheptr.h < prev    next >
C/C++ Source or Header  |  1999-03-14  |  5KB  |  133 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- //
  5. // C++ Header File Name: cacheptr.h
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer   
  8. // File Creation Date: 02/07/1997  
  9. // Date Last Modified: 03/15/1999
  10. // Copyright (c) 1997 Douglas M. Gaer
  11. // ----------------------------------------------------------- // 
  12. // ---------- Include File Description and Details  ---------- // 
  13. // ----------------------------------------------------------- // 
  14. /*
  15. The VBD C++ classes are copyright (c) 1997, by Douglas M. Gaer.
  16. All those who put this code or its derivatives in a commercial
  17. product MUST mention this copyright in their documentation for
  18. users of the products in which this code or its derivative
  19. classes are used. Otherwise, you have the freedom to redistribute
  20. verbatim copies of this source code, adapt it to your specific
  21. needs, or improve the code and release your improvements to the
  22. public provided that the modified files carry prominent notices
  23. stating that you changed the files and the date of any change.
  24.  
  25. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  26. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  27. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  28. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  29. CORRECTION.
  30.  
  31. Cache pointers are used to work in conjunction with the
  32. reference counted cache buckets. Each cache pointer stores the
  33. file address of the object being poined to, a pointer to the
  34. bucket containing the memory buffer of the object, and a pointer
  35. to the cache the cache pointer is connected to.
  36. */
  37. // ----------------------------------------------------------- //   
  38. #ifndef __CACHEPTR_HPP__
  39. #define __CACHEPTR_HPP__
  40.  
  41. #include "chptrb.h"
  42. #include "cache.h"
  43. #include "placenew.h"
  44.  
  45. // NOTE: To avoid portability problems with template classes, enable
  46. // the __NOT_USING_TEMPLATE_CLASS__ macro and directly code the
  47. // Bucket class, Cache class, and the CachePtr class for the data
  48. // type that will be used.
  49.  
  50. // Default to using template class
  51. #ifndef __NOT_USING_TEMPLATE_CLASS__
  52. #define __USING_TEMPLATE_CLASS__
  53. #endif
  54.  
  55. #ifdef __USING_TEMPLATE_CLASS__
  56. // (C)ache (P)ointer class
  57. template<class TYPE>
  58. class CachePtr : public CachePtrb
  59. {
  60. public:
  61.   CachePtr(Cache<TYPE> &c, FAU p = 0) : CachePtrb(c, p) { }
  62.   CachePtr(const CachePtr<TYPE> &c) : CachePtrb(c) { }
  63.   void operator=(const CachePtr<TYPE> &c) { Copy(c); }
  64.   void operator=(__LWORD__ p) { Release(); Address = p; }
  65.  
  66. public:
  67.   void Delete() { CachePtrb::Delete(sizeof(TYPE)); }
  68.  
  69.   Bucket<TYPE> &operator*() {
  70.     if(!bound) Bind(); return *(Bucket<TYPE> *)bkt; }
  71.  
  72.   Bucket<TYPE> *operator->() {
  73.     if (!bound) Bind(); return (Bucket<TYPE> *)bkt; }
  74.  
  75.   friend void *operator new(size_t n, CachePtr<TYPE> &cp, __LWORD__ p = 0) 
  76.   // Allocates a new object to reside at address p in the file.
  77.   // If p is non-zero, the object is presumed to already
  78.   // allocated. The allocation takes place by calling Alloc(),
  79.   // which not only allocates the object on disk, but also
  80.   // reserves a cache bucket for it. A Bucketb pointer to this
  81.   // bucket is returned by Alloc(), which is typecast as a
  82.   // Bucket<TYPE> pointer. Then a typecast to a TYPE pointer,
  83.   // retrieves a pointer to the bucket's data. The pointer is
  84.   // returned, and is passed to the associated TYPE constructor
  85.   // as the 'this' pointer. 
  86.   {
  87.     return (TYPE *)((Bucket<TYPE> *)(cp.Alloc(n, p))); 
  88.   }
  89. };
  90. #endif // __USING_TEMPLATE_CLASS__
  91.  
  92. #ifdef __NOT_USING_TEMPLATE_CLASS__
  93. // (C)ache (P)ointer class
  94. class CachePtr : public CachePtrb
  95. {
  96. public:
  97.   CachePtr(Cache &c, FAU p = 0) : CachePtrb(c, p) { }
  98.   CachePtr(const CachePtr &c) : CachePtrb(c) { }
  99.   void operator=(const CachePtr &c) { Copy(c); }
  100.   void operator=(__LWORD__ p) { Release(); Address = p; }
  101.  
  102. public:
  103.   void Delete() { CachePtrb::Delete(sizeof(TYPE)); }
  104.  
  105.   Bucket &operator*() {
  106.     if(!bound) Bind(); return *(Bucket *)bkt; }
  107.  
  108.   Bucket *operator->() {
  109.     if (!bound) Bind(); return (Bucket *)bkt; }
  110.  
  111.   friend void *operator new(size_t n, CachePtr &cp, __LWORD__ p = 0) 
  112.   // Allocates a new object to reside at address p in the file.
  113.   // If p is non-zero, the object is presumed to already
  114.   // allocated. The allocation takes place by calling Alloc(),
  115.   // which not only allocates the object on disk, but also
  116.   // reserves a cache bucket for it. A Bucketb pointer to this
  117.   // bucket is returned by Alloc(), which is typecast as a
  118.   // Bucket pointer. Then a typecast to a TYPE pointer,
  119.   // retrieves a pointer to the bucket's data. The pointer is
  120.   // returned, and is passed to the associated TYPE constructor
  121.   // as the 'this' pointer. 
  122.   {
  123.     return (TYPE *)((Bucket *)(cp.Alloc(n, p))); 
  124.   }
  125. };
  126. #endif //  __NOT_USING_TEMPLATE_CLASS__
  127.  
  128. #endif // __CACHEPTR_HPP__
  129. // ----------------------------------------------------------- // 
  130. // ------------------------------- //
  131. // --------- End of File --------- //
  132. // ------------------------------- //
  133.